package com.wc.算法基础课.C第三讲搜索与图论.prim.Prim算法求最小生成树;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/15 13:06
 * @description https://www.acwing.com/problem/content/description/860/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 510, INF = 0x3f3f3f3f;
    static int[][] g = new int[N][N];
    static int[] dist = new int[N];
    static boolean[] st = new boolean[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            Arrays.fill(g[i], 1, n + 1, INF);
            g[i][i] = 0;
        }
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            g[a][b] = g[b][a] = Math.min(g[a][b], c);
        }
        int res = prim();
        if (res == INF) out.println("impossible");
        else out.println(res);
        out.flush();
    }

    static int prim() {
        Arrays.fill(dist, 1, n + 1, INF);
        int res = 0;
        for (int i = 1; i <= n; i++) {
            // 选择一个最短路
            int t = -1;
            for (int j = 1; j <= n; j++) {
                if (!st[j] && (t == -1 || dist[t] > dist[j])) {
                    t = j;
                }
            }
            // 如果说没有任何一个边连接到最短路,那就说明图不连通
            if (i > 1 && dist[t] == INF) return INF;
            if (i > 1) res += dist[t];
            // 更新和这个点连接的最短路
            for (int j = 1; j <= n; j++) dist[j] = Math.min(dist[j], g[t][j]);
            // 将连接的点加入集合
            st[t] = true;
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
